using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System.IO;

public class ImageToPdfConverter
{
    public void ConvertImageToPdf(string imagePath, string outputPdfPath)
    {
        // 创建PDF文档
        PdfDocument document = new PdfDocument();
        
        // 加载图片并调整页面尺寸
        XImage image = XImage.FromFile(imagePath);
        XSize pageSize = new XSize(image.PixelWidth, image.PixelHeight);
        PdfPage page = document.AddPage();
        page.Width = pageSize.Width;
        page.Height = pageSize.Height;

        // 绘制图片到PDF页面
        XGraphics gfx = XGraphics.FromPdfPage(page);
        gfx.DrawImage(image, 0, 0, pageSize.Width, pageSize.Height);

        // 保存PDF
        document.Save(outputPdfPath);
        document.Close();
    }

    // 批量转换示例
    public void BatchConvertImagesToPdf(string[] imagePaths, string outputPdfPath)
    {
        PdfDocument document = new PdfDocument();
        foreach (var imagePath in imagePaths)
        {
            XImage image = XImage.FromFile(imagePath);
            PdfPage page = document.AddPage();
            page.Width = image.PixelWidth;
            page.Height = image.PixelHeight;
            XGraphics gfx = XGraphics.FromPdfPage(page);
            gfx.DrawImage(image, 0, 0, page.Width, page.Height);
        }
        document.Save(outputPdfPath);
        document.Close();
    }
}
